//
//  LiveStreamDataViewController.m
//  YouinRTCDemo
//
//  Created by 潘欣 on 2025/5/26.
//

#import "LiveStreamDataViewController.h"
#import "AFNetworking.h"
#import "LiveListViewController.h"
@interface LiveStreamDataViewController ()
@property (nonatomic,strong)UILabel *fangwenLabel;
@property (nonatomic,strong)UILabel *wenLabel;//总访问人次
@property (nonatomic,strong)UILabel *peopleLabel;//总访问人数
@property (nonatomic,strong)UILabel *watchLabel;//总观看人数

@property (nonatomic,strong)UILabel *rateLabel;//转化率
@property (nonatomic,strong)UILabel *timeLabel;//观看时长
@property (nonatomic,strong)UILabel *onlineLabel;//最高在线


///分享
@property (nonatomic,strong)UILabel *shareLabel;
@property (nonatomic,strong)UILabel *countLabel;//分享次数
@property (nonatomic,strong)UILabel *inviteLabel;//邀请人数
@property (nonatomic,strong)UILabel *goodLabel;//有效邀请人数
@property (nonatomic,strong)UILabel *newsLabel;//本场拉新

@property (nonatomic,strong)UILabel *hudongLabel;//互动

@property (nonatomic,strong)UILabel *likeLabel;//点击次数
@property (nonatomic,strong)UILabel *redLabel;//红包总额
@property (nonatomic,strong)UILabel *giftsLabel;//礼物收益



@property (nonatomic,strong)UILabel *tradeLabel;//交易
@property (nonatomic,strong)UILabel *clickLabel;//点击次数
@property (nonatomic,strong)UILabel *goodsLabel;//商品点击次数
@property (nonatomic,strong)UILabel *purchaseLabel;//购买人数
@property (nonatomic,strong)UILabel *ordersLabel;//订单数
@property (nonatomic,strong)UILabel *moneyLabel;//累计订单金额
@property (nonatomic,strong)UILabel *refoundLabel;//成功退款金额
@end

@implementation LiveStreamDataViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:true];
   
}
- (void)goBackToPreview
{
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[LiveListViewController class]]) {
            [self.navigationController popToViewController:vc animated:YES];
            return;
        }
    }
    // 如果没找到，默认 pop 回上一页
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"直播数据";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *buttonBack=[UIButton buttonWithType:UIButtonTypeCustom];
    buttonBack.frame =  CGRectMake(0, 0, 50, 50);
    [buttonBack setImage:[UIImage imageNamed:@"nav-return"] forState:UIControlStateNormal];
    [buttonBack addTarget:self action:@selector(goBackToPreview) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:buttonBack];
    buttonBack.imageEdgeInsets = UIEdgeInsetsMake(0, -27, 0, 0);
    self.navigationItem.leftBarButtonItem = backItem;
    

    
    UIView *fangwenView = [[UIView alloc] init];
    fangwenView.backgroundColor = [UIColor whiteColor];
    fangwenView.layer.cornerRadius = 5;
    fangwenView.clipsToBounds = YES;
    [self.view addSubview:fangwenView];
    
    [fangwenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view).offset(10);
        make.right.mas_equalTo(self.view).offset(-10);
//        make.top.mas_equalTo(self.view).offset(20);
        make.top.mas_equalTo(self.view).offset(YPW_kStatusBarHeight+kNavBarHeight);
        make.height.offset(160);
    }];
    
    self.fangwenLabel = [[UILabel alloc] init];
    self.fangwenLabel.textColor = RYColorForHex(@"#000000");;
    self.fangwenLabel.text = @"访问";
    self.fangwenLabel.font = [UIFont systemFontOfSize:18];
    [fangwenView addSubview:self.fangwenLabel];
    
    [self.fangwenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(fangwenView).offset(10);
    }];
    
    self.wenLabel = [[UILabel alloc] init];
    self.wenLabel.textColor = RYColorForHex(@"#000000");;
    self.wenLabel.text = @"总访问人次\n0";
    self.wenLabel.numberOfLines = 0;
    self.wenLabel.textAlignment = NSTextAlignmentCenter;
    self.wenLabel.font = [UIFont systemFontOfSize:12];
    [fangwenView addSubview:self.wenLabel];
    
    [self.wenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(fangwenView).offset(10);
        make.top.mas_equalTo(self.fangwenLabel.mas_bottom).offset(20);
    }];
    
    
    self.peopleLabel = [[UILabel alloc] init];
    self.peopleLabel.textColor = RYColorForHex(@"#000000");
    self.peopleLabel.text = @"总访问人数\n0";
    self.peopleLabel.numberOfLines = 0;
    self.peopleLabel.textAlignment = NSTextAlignmentCenter;
    self.peopleLabel.font = [UIFont systemFontOfSize:12];
    [fangwenView addSubview:self.peopleLabel];
    
    [self.peopleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(fangwenView);
        make.top.mas_equalTo(self.fangwenLabel.mas_bottom).offset(20);
    }];
    
    self.watchLabel = [[UILabel alloc] init];
    self.watchLabel.textColor = RYColorForHex(@"#000000");;
    self.watchLabel.text = @"总观看人数\n0";
    self.watchLabel.numberOfLines = 0;
    self.watchLabel.textAlignment = NSTextAlignmentCenter;
    self.watchLabel.font = [UIFont systemFontOfSize:12];
    [fangwenView addSubview:self.watchLabel];
    
    [self.watchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(fangwenView).offset(-10);
        make.top.mas_equalTo(self.fangwenLabel.mas_bottom).offset(20);
    }];
    
    
    self.rateLabel = [[UILabel alloc] init];
    self.rateLabel.textColor = RYColorForHex(@"#000000");;
    self.rateLabel.text = @"观看转化率\n0";
    self.rateLabel.numberOfLines = 0;
    self.rateLabel.textAlignment = NSTextAlignmentCenter;
    self.rateLabel.font = [UIFont systemFontOfSize:12];
    [fangwenView addSubview:self.rateLabel];
    
    [self.rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(fangwenView).offset(10);
        make.bottom.mas_equalTo(fangwenView.mas_bottom).offset(-20);
    }];
    
    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.textColor = RYColorForHex(@"#000000");;
    self.timeLabel.text = @"人均观看时长\n0";
    self.timeLabel.numberOfLines = 0;
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    [fangwenView addSubview:self.timeLabel];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(fangwenView);
        make.bottom.mas_equalTo(fangwenView.mas_bottom).offset(-20);
    }];
    
    
    self.onlineLabel = [[UILabel alloc] init];
    self.onlineLabel.textColor = RYColorForHex(@"#000000");;
    self.onlineLabel.text = @"最高在线\n0";
    self.onlineLabel.numberOfLines = 0;
    self.onlineLabel.textAlignment = NSTextAlignmentCenter;
    self.onlineLabel.font = [UIFont systemFontOfSize:12];
    [fangwenView addSubview:self.onlineLabel];
    
    [self.onlineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(fangwenView).offset(-10);
        make.bottom.mas_equalTo(fangwenView.mas_bottom).offset(-20);
    }];
    
    
    
    //分享
    UIView *shareView = [[UIView alloc] init];
    shareView.backgroundColor = [UIColor whiteColor];
    shareView.layer.cornerRadius = 5;
    shareView.clipsToBounds = YES;
    [self.view addSubview:shareView];
    
    [shareView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(fangwenView);
        make.top.mas_equalTo(fangwenView.mas_bottom).offset(20);
        make.height.offset(110);
    }];
    
    
    
    
    self.shareLabel = [[UILabel alloc] init];
    self.shareLabel.textColor = RYColorForHex(@"#000000");;
    self.shareLabel.text = @"分享";
    self.shareLabel.font = [UIFont systemFontOfSize:18];
    [shareView addSubview:self.shareLabel];
    
    [self.shareLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(shareView).offset(10);
    }];
    
    self.countLabel = [[UILabel alloc] init];
    self.countLabel.textColor = RYColorForHex(@"#000000");;
    self.countLabel.text = @"分享次数\n0";
    self.countLabel.numberOfLines = 0;
    self.countLabel.textAlignment = NSTextAlignmentCenter;
    self.countLabel.font = [UIFont systemFontOfSize:12];
    [shareView addSubview:self.countLabel];
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(shareView).offset(20);
        make.bottom.mas_equalTo(shareView.mas_bottom).offset(-20);
    }];
    
    
    
    self.newsLabel = [[UILabel alloc] init];
    self.newsLabel.textColor = RYColorForHex(@"#000000");;
    self.newsLabel.text = @"本场拉新\n0";
    self.newsLabel.numberOfLines = 0;
    self.newsLabel.textAlignment = NSTextAlignmentCenter;
    self.newsLabel.font = [UIFont systemFontOfSize:12];
    [shareView addSubview:self.newsLabel];
    
    [self.newsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(shareView).offset(-20);
        make.bottom.mas_equalTo(shareView.mas_bottom).offset(-20);
    }];
    
    
    self.inviteLabel = [[UILabel alloc] init];
    self.inviteLabel.textColor = RYColorForHex(@"#000000");;
    self.inviteLabel.text = @"邀请人数\n0";
    self.inviteLabel.numberOfLines = 0;
    self.inviteLabel.textAlignment = NSTextAlignmentCenter;
    self.inviteLabel.font = [UIFont systemFontOfSize:12];
    [shareView addSubview:self.inviteLabel];
    
    [self.inviteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.countLabel.mas_right).offset(40);
        make.bottom.mas_equalTo(shareView.mas_bottom).offset(-20);
    }];
    
    
    
    self.goodLabel = [[UILabel alloc] init];
    self.goodLabel.textColor = RYColorForHex(@"#000000");;
    self.goodLabel.text = @"有效邀请人数\n0";
    self.goodLabel.numberOfLines = 0;
    self.goodLabel.textAlignment = NSTextAlignmentCenter;
    self.goodLabel.font = [UIFont systemFontOfSize:12];
    [shareView addSubview:self.goodLabel];
    
    [self.goodLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.newsLabel.mas_left).offset(-40);
        make.bottom.mas_equalTo(shareView.mas_bottom).offset(-20);
    }];
    
    
    //互动
    UIView *hudongView = [[UIView alloc] init];
    hudongView.backgroundColor = [UIColor whiteColor];
    hudongView.layer.cornerRadius = 5;
    hudongView.clipsToBounds = YES;
    [self.view addSubview:hudongView];
    
    [hudongView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(shareView);
        make.top.mas_equalTo(shareView.mas_bottom).offset(20);
        make.height.offset(110);
    }];
    
    
    
    
    self.hudongLabel = [[UILabel alloc] init];
    self.hudongLabel.textColor = RYColorForHex(@"#000000");;
    self.hudongLabel.text = @"互动";
    self.hudongLabel.font = [UIFont systemFontOfSize:18];
    [hudongView addSubview:self.hudongLabel];
    [self.hudongLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(hudongView).offset(10);
    }];
    
    
    
    self.likeLabel = [[UILabel alloc] init];
    self.likeLabel.textColor = RYColorForHex(@"#000000");;
    self.likeLabel.text = @"点赞次数\n0";
    self.likeLabel.numberOfLines = 0;
    self.likeLabel.textAlignment = NSTextAlignmentCenter;
    self.likeLabel.font = [UIFont systemFontOfSize:12];
    [hudongView addSubview:self.likeLabel];
    
    [self.likeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(hudongView).offset(10);
        make.top.mas_equalTo(self.hudongLabel.mas_bottom).offset(20);
    }];
    
    
    self.redLabel = [[UILabel alloc] init];
    self.redLabel.textColor = RYColorForHex(@"#000000");;
    self.redLabel.text = @"红包总额（元）\n0";
    self.redLabel.numberOfLines = 0;
    self.redLabel.textAlignment = NSTextAlignmentCenter;
    self.redLabel.font = [UIFont systemFontOfSize:12];
    [hudongView addSubview:self.redLabel];
    
    [self.redLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(hudongView);
        make.top.mas_equalTo(self.hudongLabel.mas_bottom).offset(20);
    }];
    
    self.giftsLabel = [[UILabel alloc] init];
    self.giftsLabel.textColor = RYColorForHex(@"#000000");;
    self.giftsLabel.text = @"礼物收益（元）\n0";
    self.giftsLabel.numberOfLines = 0;
    self.giftsLabel.textAlignment = NSTextAlignmentCenter;
    self.giftsLabel.font = [UIFont systemFontOfSize:12];
    [hudongView addSubview:self.giftsLabel];
    
    [self.giftsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(hudongView).offset(-10);
        make.top.mas_equalTo(self.hudongLabel.mas_bottom).offset(20);
    }];
    
    
    
    
    //交易
    UIView *tradeView = [[UIView alloc] init];
    tradeView.backgroundColor = [UIColor whiteColor];
    tradeView.layer.cornerRadius = 5;
    tradeView.clipsToBounds = YES;
    [self.view addSubview:tradeView];
    
    [tradeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(hudongView);
        make.top.mas_equalTo(hudongView.mas_bottom).offset(20);
        make.height.offset(160);
    }];
    
    
    self.tradeLabel = [[UILabel alloc] init];
    self.tradeLabel.textColor = RYColorForHex(@"#000000");;
    self.tradeLabel.text = @"交易";
    self.tradeLabel.font = [UIFont systemFontOfSize:18];
    [tradeView addSubview:self.tradeLabel];
    [self.tradeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(tradeView).offset(10);
    }];
    
    self.clickLabel = [[UILabel alloc] init];
    self.clickLabel.textColor = RYColorForHex(@"#000000");;
    self.clickLabel.text = @"商品点击次数\n0";
    self.clickLabel.numberOfLines = 0;
    self.clickLabel.textAlignment = NSTextAlignmentCenter;
    self.clickLabel.font = [UIFont systemFontOfSize:12];
    [tradeView addSubview:self.clickLabel];
    
    [self.clickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(tradeView).offset(10);
        make.top.mas_equalTo(self.tradeLabel.mas_bottom).offset(20);
    }];
    
    
    self.goodsLabel = [[UILabel alloc] init];
    self.goodsLabel.textColor = RYColorForHex(@"#000000");;
    self.goodsLabel.text = @"商品点击人数\n0";
    self.goodsLabel.numberOfLines = 0;
    self.goodsLabel.textAlignment = NSTextAlignmentCenter;
    self.goodsLabel.font = [UIFont systemFontOfSize:12];
    [tradeView addSubview:self.goodsLabel];
    
    [self.goodsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(tradeView);
        make.top.mas_equalTo(self.tradeLabel.mas_bottom).offset(20);
    }];
    
    
    self.purchaseLabel = [[UILabel alloc] init];
    self.purchaseLabel.textColor = RYColorForHex(@"#000000");;
    self.purchaseLabel.text = @"购买人数\n0";
    self.purchaseLabel.numberOfLines = 0;
    self.purchaseLabel.textAlignment = NSTextAlignmentCenter;
    self.purchaseLabel.font = [UIFont systemFontOfSize:12];
    [tradeView addSubview:self.purchaseLabel];
    
    [self.purchaseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(tradeView).offset(-10);
        make.top.mas_equalTo(self.tradeLabel.mas_bottom).offset(20);
    }];
    
    
    self.ordersLabel = [[UILabel alloc] init];
    self.ordersLabel.textColor = RYColorForHex(@"#000000");;
    self.ordersLabel.text = @"订单数\n0";
    self.ordersLabel.numberOfLines = 0;
    self.ordersLabel.textAlignment = NSTextAlignmentCenter;
    self.ordersLabel.font = [UIFont systemFontOfSize:12];
    [tradeView addSubview:self.ordersLabel];
    
    [self.ordersLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(tradeView).offset(10);
        make.bottom.mas_equalTo(tradeView.mas_bottom).offset(-20);
    }];
    
    self.moneyLabel = [[UILabel alloc] init];
    self.moneyLabel.textColor = RYColorForHex(@"#000000");;
    self.moneyLabel.text = @"累计订单金额（元）\n0";
    self.moneyLabel.numberOfLines = 0;
    self.moneyLabel.textAlignment = NSTextAlignmentCenter;
    self.moneyLabel.font = [UIFont systemFontOfSize:12];
    [tradeView addSubview:self.moneyLabel];
    
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(tradeView);
        make.bottom.mas_equalTo(tradeView.mas_bottom).offset(-20);
    }];
    
    self.refoundLabel = [[UILabel alloc] init];
    self.refoundLabel.textColor = RYColorForHex(@"#000000");;
    self.refoundLabel.text = @"成功退款金额（元）\n0";
    self.refoundLabel.numberOfLines = 0;
    self.refoundLabel.textAlignment = NSTextAlignmentCenter;
    self.refoundLabel.font = [UIFont systemFontOfSize:12];
    [tradeView addSubview:self.refoundLabel];
    
    [self.refoundLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(tradeView).offset(-10);
        make.bottom.mas_equalTo(tradeView.mas_bottom).offset(-20);
    }];
    
    
    
    UILabel *dataLabel = [[UILabel alloc] init];
    dataLabel.textColor = [UIColor grayColor];
    dataLabel.text = @"更多数据请前往电脑端查看";
    dataLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:dataLabel];
    
    [dataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tradeView.mas_bottom).offset(10);
        make.left.mas_equalTo(tradeView).offset(10);
    }];
    
        
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 30.0f;
   manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/javascript",@"text/plain", nil];
    NSString *urlString = [NSString stringWithFormat:@"%@v1/statistics/system/analysis/%@/?&new_shop=1",HTTPSURL,self.liveid];
    NSDictionary *dict =  [[YouinRTCManager shardManager] readNSUserDefaults];
    __weak typeof(self) weakSelf = self;
    NSString *token = [NSString stringWithFormat:@"JWT %@",dict[@"token"]];
    [manager GET:urlString parameters:nil headers:@{@"authorization":token} progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            
            [weakSelf liveData:responseObject[@"result"]];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)liveData:(NSDictionary *)data
{
    self.wenLabel.text = [NSString stringWithFormat:@"总访问人次\n%@",data[@"pv_count"]];
    self.peopleLabel.text = [NSString stringWithFormat:@"总访问人数\n%@",data[@"uv_count"]];
    self.watchLabel.text = [NSString stringWithFormat:@"总观看人数\n%@",data[@"watchUserCount"]];
    self.rateLabel.text = [NSString stringWithFormat:@"观看转化率\n%.1f%%",[data[@"watchUserCount"] floatValue]/[data[@"uv_count"] floatValue]*100];
    self.timeLabel.text = [NSString stringWithFormat:@"人均观看时长\n%@",data[@"agv_duration"]];
    self.onlineLabel.text = [NSString stringWithFormat:@"最高在线\n%@",data[@"max_watch_count"]];
    
    self.countLabel.text = [NSString stringWithFormat:@"分享次数\n%@",data[@"share_count"]];
    self.inviteLabel.text = [NSString stringWithFormat:@"邀请人数\n%@",data[@"valid_share_count"]];
    self.goodLabel.text = [NSString stringWithFormat:@"有效邀请人数\n%@",data[@"invite_user_count"]];
    self.newsLabel.text = [NSString stringWithFormat:@"本场拉新\n%@",data[@"new_user_count"]];
    
    
    self.likeLabel.text = [NSString stringWithFormat:@"点赞次数\n%@",data[@"like_count"]];
    self.redLabel.text = [NSString stringWithFormat:@"红包总额（元）\n%@",data[@"red_packet_amount"]];
    self.giftsLabel.text = [NSString stringWithFormat:@"礼物收益（元）\n%@",data[@"gift_amount"]];
    
    
    self.clickLabel.text = [NSString stringWithFormat:@"商品点击次数\n%@",data[@"click_product_count"]];
    self.goodsLabel.text = [NSString stringWithFormat:@"商品点击人数\n%@",data[@"click_product_user_count"]];
    self.purchaseLabel.text = [NSString stringWithFormat:@"购买人数\n%@",data[@"statistics_trade_live"][@"userPayCount"]];
    self.ordersLabel.text = [NSString stringWithFormat:@"订单数\n%@",data[@"statistics_trade_live"][@"orderPayCount"]];
    self.moneyLabel.text = [NSString stringWithFormat:@"累计订单金额（元）\n%@",data[@"statistics_trade_live"][@"orderPayPrice"]];
    self.refoundLabel.text = [NSString stringWithFormat:@"成功退款金额（元）\n%@",data[@"statistics_trade_live"][@"afterSaleRefundPrice"]];
}
@end
